package xin.marcher.module.label.api.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.exception.ProductBizException;
import xin.marcher.module.label.api.LabelGroupApi;
import xin.marcher.module.label.domain.dto.LabelGroupDTO;
import xin.marcher.module.label.domain.dto.LabelGroupResultDTO;
import xin.marcher.module.label.domain.request.LabelGroupRequest;
import xin.marcher.module.label.domain.request.QueryLabelGroupRequest;
import xin.marcher.module.label.service.LabelGroupService;

import java.util.List;

/**
 * 标签组操作API
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = LabelGroupApi.class, retries = 0)
public class LabelGroupApiImpl implements LabelGroupApi {

    @Autowired
    private LabelGroupService labelGroupService;

    /**
     * 添加标签组接口
     *
     * @param request xin.marcher.module.label.domain.request.LabelGroupRequest
     */
    @Override
    public JsonResult<LabelGroupResultDTO> saveLabelGroup(LabelGroupRequest request) {
        try {
            LabelGroupResultDTO resultDTO = labelGroupService.saveLabelGroup(request);
            return JsonResult.buildSuccess(resultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 根据条件查询标签组列表接口（标签组数量很少不需要分页）
     *
     * @param request xin.marcher.module.label.domain.request.QueryLabelGroupRequest
     */
    @Override
    public JsonResult<List<LabelGroupDTO>> listLabelGroups(QueryLabelGroupRequest request) {
        try {
            List<LabelGroupDTO> resultDTO = labelGroupService.listLabelGroups(request);
            return JsonResult.buildSuccess(resultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
